<template>
  <div v-if="is_info" class="text-ellipsis">{{value}}</div>
  <div v-else class="form_item-wechat_cert">
    <input type="file" style="display: none" ref="input_file" >
    <text_one :name="name" :value="value" :size="size" @update:value="$emit('update:value', $event)" @change="$emit('change', $event)"></text_one>
    <p>1、证书编号如果不清楚，可以传入证书文件获取，文件名一般为：apiclient_cert.pem，<el-button size="small" @click="upload_cert">点击上传</el-button> </p>
    <p>2、如果提示：商户私钥不存在，还需要上传私钥文件（上传私钥文件需要先配置证书编号），文件名一般为：apiclient_key.pem，<el-button size="small" @click="upload_key">点击上传</el-button> </p>
  </div>
</template>

<script>
import text_one from "@/components/auto_import/UnifyParam/text_one.vue";
import {ajax} from "@/js/api_tool.js";
export default {
  _title_: '微信证书',
  _can_config_: true,
  name: "wechat_cert",
  components: {text_one},
  props: ['name', 'value', 'config', 'is_info', 'size', 'is_list'],
  emits: ['update:value', 'change'],
  data(){
    return {}
  },
  methods: {
    upload_cert(){
      let app = this;
      let input_dom = app.$refs['input_file'];
      input_dom.onchange = function (){
        let f = this.files[0];
        let fd = new FormData();
        fd.append('input_file', f);
        ajax({url:'/admin/api/certificate_no',data:fd,method:'POST'}, function (res){
          app.$emit('update:value', res.data);
          app.$emit('change', res.data);
        })
        input_dom.value = '';
      }
      input_dom.click();
    },
    upload_key(){
      let app = this;
      if (!app.value) return app.$message.error('证书编号为空，请先填入证书编号');
      let input_dom = app.$refs['input_file'];
      input_dom.onchange = function (){
        let f = this.files[0];
        let fd = new FormData();
        fd.append('input_file', f);
        ajax({url:'/admin/api/upload_key?certificate_no='+app.value,data:fd,method:'POST'}, function (res){
          app.$message.success('商户私钥上传成功');
          return false;
        })
        input_dom.value = '';
      }
      input_dom.click();
    },
  },
}
</script>

<style scoped>

</style>